package com.office.supplies.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.office.supplies.pojo.Sale;
import com.office.supplies.pojo.dto.SelectSaleListDTO;
import com.office.supplies.pojo.vo.SaleGoodsVO;
import com.office.supplies.pojo.vo.SaleListVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @Author admin
 * @Description TODO
 * @Date 2023/12/12/12:58
 */
@Mapper
public interface SaleMapper extends BaseMapper<Sale> {

    /**
     * 获取商品信息和库存
     *
     * @return List<SaleGoodsVO>
     */
    @Select("SELECT goods_id, goods_name, goods_count FROM stock " +
            "LEFT JOIN goods ON stock.goods_id = goods.id")
    List<SaleGoodsVO> getGoodsListAndCount();

    /**
     * 插入一条销售信息
     *
     * @param sale Sale
     */
    @Insert("insert into sale (goods_id, emp_id, sale_count, price, total_price) " +
            "value ( #{goodsId},#{empId},#{saleCount},#{price},#{totalPrice} )")
    void insertSale(Sale sale);

    /**
     * 查询销售订单
     *
     * @param selectSaleListDTO SelectSaleListDTO
     * @return List<SaleListVO>
     */
    List<SaleListVO> selectSaleInfos(SelectSaleListDTO selectSaleListDTO);

    /**
     * 修改销售订单
     *
     * @param sale Sale
     */
    @Update("update sale set price = #{price}, total_price = #{totalPrice} where id = #{id}")
    void updateSaleInfo(Sale sale);

    /**
     * 删除销售订单
     *
     * @param id Long
     */
    @Delete("delete from sale where id = #{id}")
    void deleteById(Long id);

    /**
     * 根据goods_id删除销售订单
     *
     * @param goodsId Long
     */
    @Delete("delete from sale where goods_id = #{goodsId}")
    void deleteByGoodsId(Long goodsId);


    /**
     * 查询某个商品是否有销售记录
     *
     * @param goodsId Long
     * @return int
     */
    @Select("select count(id) from sale where goods_id = #{goodsId}")
    int selectSaleCountByGoodsId(Long goodsId);

    /**
     * 获取销售订单的所有goods_id
     *
     * @return HashSet<Integer>
     */
    @Select("select distinct goods_id from sale")
    List<Long> getSaleGoodsIds();

    /**
     * 根据商品goodsId查询销售商品的名字和数量
     *
     * @param goodsId Long
     * @return Map<String,Integer>
     */
    Map<String,Integer> getGoodsNameAndSaleCountByGoodsId(Long goodsId);

    /**
     * 查询订单数
     *
     * @return int
     */
    @Select("select count(id) from sale")
    int getSaleOrderCount();

    @Select("select count(goods_id) from warehouse.sale where goods_id = #{goodsId}")
    int selectSaleByGoodsId(Long goodsId);

}
